package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.ShareTemplate;
import cn.nubia.admin.application.query.ShareTemplateQuery;
import cn.nubia.admin.application.service.ShareTemplateService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.bean.DictFieldItem;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * TemplateController
 *
 * @author nb
 * @date 202015/7/14
 * @since V1.0
 */
@Controller
@RequestMapping("/admin/template")
public class ShareTemplateController {
    private final static Logger logger = LoggerFactory.getLogger(ShareTemplateController.class);
    @Autowired
    private ShareTemplateService shareTemplateService;

    /**
     * 新增模板接口
     *
     * @param template 模板对象
     * @return 新增的模板对象.
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<ShareTemplate> saveTemplate(@Valid ShareTemplate template,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.getFailedResponse(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            shareTemplateService.save(template);
        }catch (Exception e) {
            logger.error("保存模板异常:参数:"+template.toString(), e);
        }
        return Response.getSuccessResponse(template);
      //  return "redirect:/admin/template/list.html";
    }
    /**
     * 新增模板接口
     *
     * @param parentId 模板id
     * @return 新增的模板对象.
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addTemplate(Integer parentId) {
        ModelAndView modelAndView = new ModelAndView("/template/add");
        try {
            modelAndView.addObject("template",new ShareTemplate());
            List<DictFieldItem> fieldItems = shareTemplateService.getTemplateTypeList();
            modelAndView.addObject("fieldItems",fieldItems);
        }catch (Exception e) {
            logger.error("新增模板异常:参数:"+parentId, e);
        }
        return modelAndView;
    }
    /**
     * 查看模板
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/template/add");
        ShareTemplate shareTemplate =null;
        try {
            shareTemplate =  shareTemplateService.viewShareTemplat(id);
            List<DictFieldItem> fieldItems = shareTemplateService.getTemplateTypeList();
            modelAndView.addObject("fieldItems",fieldItems);
            modelAndView.addObject("template",shareTemplate);
        }  catch (Exception e) {
            logger.error("查看模板异常:参数"+id, e);
        }
        return modelAndView;
    }

    /**
     * 模板列表
     * @param shareTemplateQuery
     * @return
     */
    @RequiresPermissions(value = "soft:appcfg:template:list")
    @RequestMapping(value ="/list", method = RequestMethod.GET)
    public ModelAndView list(ShareTemplateQuery shareTemplateQuery) {
        //参数不正确
        Paged<ShareTemplate> paged=null;
        ModelAndView modelAndView = new ModelAndView("/template/list");
        try {
            paged = shareTemplateService.list(shareTemplateQuery);
            List<DictFieldItem> fieldItems = shareTemplateService.getTemplateTypeList();
            modelAndView.addObject("fieldItems",fieldItems);
            modelAndView.addObject("pageList",paged);
            modelAndView.addObject("query",shareTemplateQuery);
        }  catch (Exception e) {
            logger.error("模板查询异常:参数:"+shareTemplateQuery.toString(), e);
        }
        return modelAndView;
    }

    /**
     * 删除模板
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response<Boolean> delete(Integer id) {
        try {
            shareTemplateService.delete(id);
        }  catch (Exception e) {
            logger.error("删除模板异常:参数"+id, e);
            return Response.getFailedResponse("删除失败",false);
        }
        return Response.getSuccessResponse(true);
    }

}
